package com.zhenxiang.juc.assist;

import java.util.concurrent.CountDownLatch;

/**
 * @Author : James
 * @Create : 2021-12-22-11:53
 * @Description : 计数器
 *   允许一个或多个线程等待直到在其他线程中执行的一组操作完成的同步辅助
 *   每次有线程调用 countDown() 数量-1，假设计数器变为0，countDownLatch.await() 就会被唤醒，继续执行！
 *
 */
public class CountDownLatchDemo {


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"Go out ");
                // 计数器 减一
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        // 等待计数器数量归零，然后再向下执行
        countDownLatch.await();
        System.out.println("Close Door");

    }
}
